package cn.devzyh.controller;

import cn.devzyh.domain.ResultVo;
import cn.devzyh.domain.Role;
import cn.devzyh.domain.User;
import cn.devzyh.domain.UserVo;
import cn.devzyh.service.UserService;
import cn.devzyh.utils.UuidUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 用户模块处理器
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 用户分页&条件查询
    @PostMapping("/findAllUserByPage")
    public ResultVo findAllUserByPage(@RequestBody UserVo userVo) {

        PageInfo pageInfo = userService.findByConditionAndPage(userVo);

        return new ResultVo(true, 200, "响应成功", pageInfo);
    }

    // 用户状态设置
    @GetMapping("/updateUserStatus")
    public ResultVo updateUserStatus(@RequestParam(value = "id", required = true) Integer id,
                                     @RequestParam(value = "status", required = true) String status) {

        userService.updateUserStatus(id, status);
        return new ResultVo(true, 200, "用户状态修改成功", status);
    }

    // 登录信息验证
    @PostMapping("/login")
    public ResultVo login(User user, HttpServletRequest req) {

        User res = userService.login(user);
        if (null == res) {
            return new ResultVo(true, 400, "账号或密码错误", null);
        } else {

            // 生成 token
            String accessToken = UuidUtil.uuid();

            // 保存到 Session
            HttpSession session = req.getSession();
            session.setAttribute("access_token", accessToken);
            session.setAttribute("user_id", res.getId());

            // 返回数据
            Map<String, Object> map = new HashMap<>();
            map.put("access_token", accessToken);
            map.put("user_id", res.getId());

            return new ResultVo(true, 1, "登录信息验证成功", map);
        }
    }

    // 查询用户角色
    @GetMapping("/findUserRoleById")
    public ResultVo findUserRoleById(@RequestParam(value = "id", required = true) Integer userId) {

        List<Role> list = userService.findUserRoleById(userId);
        return new ResultVo(true, 200, "响应成功", list);
    }

    // 分配用户角色
    @PostMapping("/userContextRole")
    public ResultVo userContextRole(@RequestBody UserVo userVo) {

        userService.userContextRole(userVo);
        return new ResultVo(true, 200, "修改用户角色信息成功", null);
    }

    // 查询用户权限
    @GetMapping("/getUserPermissions")
    public ResultVo getUserPermissions(HttpServletRequest req) {

        // 获取携带的 token
        String headerToken = req.getHeader("Authorization");

        // 获取存储的 token
        String sessionToken = req.getSession().getAttribute("access_token").toString();

        // 对比 token
        if (sessionToken.equals(headerToken)) {

            // 获取用户账号
            int userId = Integer.parseInt(req.getSession().getAttribute("user_id").toString());
            Map<String, Object> map = userService.getUserPermissions(userId);
            return new ResultVo(true, 200, "响应成功", map);
        } else {

            return new ResultVo(true, 400, "获取用户权限信息失败", null);
        }
    }
}
